e08811fc412243379c3dfbc9410b2e26 by 2025
Author:2025
Language: eng
Format: epub
Published: 2025-03-18T00:36:28+00:00
8.4
Solved Examples
151
9
number = float(input("Enter a number: "))
10
11
# Prompt the user to enter the exponent
12
exponent = float(input("Enter the exponent: "))
13
14
# Calculate the power using the calculate_power function
15
result = calculate_power(number, exponent)
16
17
# Print the result
18
print(number, "raised to the power", exponent, "is:", result) In this program, we define a function called calculate_power that takes a base and an exponent as parameters and calculates the power of the base raised to the exponent.
In the main program, we prompt the user to enter a number and store it in a variable called ânumberâ. Next, we prompt the user for the exponent and store it in the variable called âexponentâ. Using the calculate_power function, we pass the
ânumberâ and the âexponentâ as arguments to calculate the power. The function, as in the code above, calculates the result and sends it back to the main program.
Finally, we print the result by concatenating the appropriate values.
8.4.4
Example 4: Creating a Simple Calculator
In this example, we want to create a calculator as we did before in solved example 3 in Chapter 3. The program in this example uses four functions to add, subtract, multiply, and divide. The program will be
1
# Solved example 4
2
3
def add(x, y):
4
"""Function to add two numbers"""
5
return x + y
6
7
def subtract(x, y):
8
"""Function to subtract two numbers"""
9
return x â y
10
11
def multiply(x, y):
12
"""Function to multiply two numbers"""
13
return x â y
14
15
def divide(x, y):
16
"""Function to divide two numbers"""
17
if y != 0:
18
return x / y
19
else:
20
print("Error: Cannot divide by zero")
21
22
# Main program
23
# Prompt the user to enter two numbers
24
num1 = float(input("Enter the first number: "))
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(176655)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(85076)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(84706)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(84481)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74440)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50894)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40263)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40216)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40096)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32734)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32509)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32454)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32388)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32362)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32333)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32259)
What's Done in Darkness by Kayla Perrin(27150)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26527)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26460)